package com.huitone.gddw.controller.monitoring.sms;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.common.excel.CreateExcel;
import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.log.Log;
import com.huitone.gddw.log.LogResult;
import com.huitone.gddw.log.LogType;
import com.huitone.gddw.service.dto.monitoring.sms.GateReportACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.GateReportDTO;
import com.huitone.gddw.service.dto.monitoring.sms.GateUpRepACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.GateUpRepDTO;
import com.huitone.gddw.service.monitoring.sms.IGateReportService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信通道监控
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="gateReport")
public class GateReportController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(GateReportController.class);
	
	@Autowired
	private IGateReportService gateReportService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public GateReportACDTO query(HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询短信通道监控";
		GateReportACDTO result = null;
		try {
			result = getList(request);
		} catch (Exception e) {
			LOGGER.error("查询短信通道监控出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 导出 Excel
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/out-excel", method=RequestMethod.POST)
	public void outExcel(HttpServletRequest request, HttpServletResponse response) {
		String fileName = "短信通道监控信息";
		
		GateReportACDTO re = getList(request);
		if (re != null && (null == re.getList() || 0 == re.getList().size())) {
			re.getList().add(new GateReportDTO());
		}
		
		HSSFWorkbook wb = (HSSFWorkbook)CreateExcel
				.getInstance()
				.CreateNewExcelNoTemplate(fileName, re.getList(), re).getWorkbook();
		outExt(fileName, wb, response);
	}
	
	/**
	 * 获取列表数据
	 * @param request
	 * @return AccessReportACDTO
	 */
	private GateReportACDTO getList(HttpServletRequest request) {
		Map<String, Object> params = getParams(request);
		return gateReportService.find(params);
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		return params;
	}
	
	/**
	 * 获取列表数据 上行
	 * @param request
	 * @return GateUpRepACDTO
	 */
	private GateUpRepACDTO getListUp(HttpServletRequest request) {
		Map<String, Object> params = getParams(request);
		return gateReportService.findUp(params);
	}
	
	/**
	 * 查询上行短信信息
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="queryUp")
	@Log(logType=LogType.SELECT, content="查询短信通道监控-上行短信信息")
	public GateUpRepACDTO queryUp(HttpServletRequest request) {
		try {
			GateUpRepACDTO result = getListUp(request);
			return result;
		} catch (Exception e) {
			LOGGER.error("查询短信通道监控出现异常：" + e.getMessage(), e);
			request.setAttribute("logResult", LogResult.EXCEPTION);
			return null;
		}
	}
	
	/**
	 * 导出 Excel
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/out-excel-up", method=RequestMethod.POST)
	public void outExcelUp(HttpServletRequest request, HttpServletResponse response) {
		String fileName = "短信通道监控信息";
		
		GateUpRepACDTO re = getListUp(request);
		if (re != null && (null == re.getList() || 0 == re.getList().size())) {
			re.getList().add(new GateUpRepDTO());
		}
		
		HSSFWorkbook wb = (HSSFWorkbook)CreateExcel
				.getInstance()
				.CreateNewExcelNoTemplate(fileName, re.getList(), re).getWorkbook();
		outExt(fileName, wb, response);
	}
	
	private void outExt(String fileName, HSSFWorkbook wb, HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename="+fileName+sdf.format(new Date())+".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}
	
}
